package com.ruoyi.worker.service.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.worker.domain.HealthRecord;
import com.ruoyi.worker.mapper.HealthRecordMapper;
import com.ruoyi.worker.service.IHealthRecordService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 人员健康档案Service业务层处理
 *
 * @author wng
 * @date 2024-08-04
 */
@Service
public class HealthRecordServiceImpl implements IHealthRecordService {
    @Resource
    private HealthRecordMapper healthRecordMapper;

    /**
     * 查询人员健康档案
     *
     * @param id 人员健康档案主键
     * @return 人员健康档案
     */
    @Override
    public HealthRecord selectHealthRecordById(Long id) {
        HealthRecord healthRecord = healthRecordMapper.selectHealthRecordById(id);
        String nickName = healthRecordMapper.selectSysUserNameById(healthRecord.getUserId());
        healthRecord.getParams().put("nickName", nickName);
        healthRecord.setUserName(nickName);
        return healthRecord;
    }

    /**
     * 查询人员健康档案列表
     *
     * @param healthRecord 人员健康档案
     * @return 人员健康档案
     */
    @Override
    public List<HealthRecord> selectHealthRecordList(HealthRecord healthRecord) {
        if (healthRecord.getParams() != null) {
            if (healthRecord.getParams().containsKey("createTimeStart") && healthRecord.getParams().containsKey("createTimeEnd")) {
                if (healthRecord.getParams().get("createTimeStart") != null && !"".equals(healthRecord.getParams().get("createTimeStart"))
                        && healthRecord.getParams().get("createTimeEnd") != null && !"".equals(healthRecord.getParams().get("createTimeEnd"))) {
                    healthRecord.getParams().put("createTimeStart", healthRecord.getParams().get("createTimeStart") + " 00:00:00");
                    healthRecord.getParams().put("createTimeEnd", healthRecord.getParams().get("createTimeEnd") + " 23:59:59");
                }
            }
        }
        List<HealthRecord> healthRecords = healthRecordMapper.selectHealthRecordList(healthRecord);
        healthRecords.forEach(item -> {
            if (item.getExpireTimeStart() != null && item.getExpireTimeEnd() != null) {
                item.getParams().put("expireTime", DateUtils.parseDateToStr("yyyy-MM-dd", item.getExpireTimeStart()) + " 至 " + DateUtils.parseDateToStr("yyyy-MM-dd", item.getExpireTimeEnd()));
            }
            if (item.getUserId() != null) {
                item.setUserName(healthRecordMapper.selectSysUserNameById(item.getUserId()));
            }
        });
        return healthRecords;
    }

    /**
     * 新增人员健康档案
     *
     * @param healthRecord 人员健康档案
     * @return 结果
     */
    @Override
    public int insertHealthRecord(HealthRecord healthRecord) {
        healthRecord.setCreateTime(DateUtils.getNowDate());
        //查询人员新增了几个健康档案,有没有过期的
        AtomicReference<Long> count = new AtomicReference<>(0L);
        healthRecordMapper.selectHealthRecordList(new HealthRecord() {{
            setUserId(healthRecord.getUserId());
        }}).forEach(item -> {
            if (item.getExpireTimeEnd().getTime() > System.currentTimeMillis()) {
                count.getAndSet(count.get() + 1);
            }
        });
        if (count.get() > 0) {
            throw new ServiceException("上一个健康证档案还未过期。");
        }

        Long deptId = healthRecordMapper.selectSysUserDeptIdById(healthRecord.getUserId());
        healthRecord.setDeptId(deptId);
        return healthRecordMapper.insertHealthRecord(healthRecord);
    }

    /**
     * 修改人员健康档案
     *
     * @param healthRecord 人员健康档案
     * @return 结果
     */
    @Override
    public int updateHealthRecord(HealthRecord healthRecord) {
        healthRecord.setUpdateTime(DateUtils.getNowDate());
        //查询人员新增了几个健康档案,有没有过期的
        AtomicReference<Long> count = new AtomicReference<>(0L);
        healthRecordMapper.selectHealthRecordList(new HealthRecord() {{
            setUserId(healthRecord.getUserId());
        }}).forEach(item -> {
            if (item.getExpireTimeEnd().getTime() > System.currentTimeMillis()) {
                if(!item.getId().equals(healthRecord.getId())){
                    count.getAndSet(count.get() + 1);
                }
            }
        });
        if (count.get() > 0) {
            throw new ServiceException("上一个健康证档案还未过期。");
        }
        return healthRecordMapper.updateHealthRecord(healthRecord);
    }

    /**
     * 批量删除人员健康档案
     *
     * @param ids 需要删除的人员健康档案主键
     * @return 结果
     */
    @Override
    public int deleteHealthRecordByIds(Long[] ids) {
        return healthRecordMapper.deleteHealthRecordByIds(ids);
    }

    /**
     * 删除人员健康档案信息
     *
     * @param id 人员健康档案主键
     * @return 结果
     */
    @Override
    public int deleteHealthRecordById(Long id) {
        return healthRecordMapper.deleteHealthRecordById(id);
    }
}
